//
//  BannerControlView.m
//  DetailNSNULL
//
//  Created by 蓓蕾 on 2021/7/27.
//  Copyright © 2021 YangYang. All rights reserved.
//

#import "BannerControlView.h"
//#import "UITapImageView.h"

@interface BannerControlView ()

@property (nonatomic, strong) UIView *dotBgView;

@property (nonatomic, strong) NSMutableArray *dotViewArr;

@property (nonatomic, strong) UIImageView *currentDotView;

@property (nonatomic, assign) CGFloat leftMargin;

@property (nonatomic, assign) CGFloat topMargin;

@property (nonatomic, assign) CGFloat itemmargin;

@property (nonatomic, assign) CGFloat currentDotwidth;

@property (nonatomic, assign) CGFloat currentDotheight;

@property (nonatomic, assign) CGFloat otherDotwidth;

@property (nonatomic, assign) CGFloat otherDotheight;

@property (nonatomic, strong) NSString *currentDotImageName;

@property (nonatomic, strong) NSString *otherDotImageName;

@property (nonatomic, strong) UIImage *currentDotImage;
@property (nonatomic, strong) UIImage *otherDotImage;

@end

@implementation BannerControlView

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        self.backgroundColor = [UIColor blackColor];
        _leftMargin = 10;
        _topMargin = 10;
        _itemmargin = 10;
        
        _currentDotwidth = 20;
        _currentDotheight = 20;
        _otherDotwidth = 15;
        _otherDotheight = 15;
        
        _dotBgView = [[UIView alloc] init];
        [self addSubview:_dotBgView];
//        _dotBgView.backgroundColor = [UIColor blackColor];
        [_dotBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX).mas_offset(0);
            make.bottom.mas_equalTo(self.mas_bottom).mas_offset(0);
            make.top.mas_equalTo(self.mas_top).mas_offset(0);
        }];
        
        UIView *transluView = [[UIView alloc] init];
        [self addSubview:transluView];
        transluView.backgroundColor = [UIColor blackColor];
        transluView.alpha = 0.1;
        [transluView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(_dotBgView).mas_offset(0);
        }];
    }
    return self;
}

-(void)setLocationType:(ControllerLocationType)locationType
{
    _locationType = locationType;
    [self updateControllerSize];
    if (locationType == RightCenterType) {
        [_dotBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY).mas_offset(0);
            make.right.mas_equalTo(self.mas_right).mas_offset(0);
            make.left.mas_equalTo(self.mas_left).mas_offset(0);
        }];
    }
}

-(void)setShapeType:(ControllerShapeType)shapeType
{
    _shapeType = shapeType;
    [self updateControllerSize];
}

-(void)updateControllerSize
{
    switch (_shapeType) {
        case CircleType:
        {//圆形
            _currentDotwidth = 10;
            _currentDotheight = 10;
            _otherDotwidth = 7;
            _otherDotheight = 7;
        }
            break;
        case SquareType:
        {//正方形
            _currentDotwidth = 10;
            _currentDotheight = 10;
            _otherDotwidth = 7;
            _otherDotheight = 7;
        }
            break;
        case RectangleType:
        {//长方形
            if (_locationType == BottomLeftType || _locationType == BottomCenterType || _locationType == BottomRightType) {
                _currentDotwidth = 30;
                _currentDotheight = 3;
                _otherDotwidth = 30;
                _otherDotheight = 3;
            }
            else
            {
                _currentDotwidth = 3;
                _currentDotheight = 30;
                _otherDotwidth = 3;
                _otherDotheight = 30;
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)updateDotBgViewWidth
{
    if (_locationType == RightCenterType) {
        [_dotBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            CGFloat height = _topMargin * 2 + (_dotCount - 1) * _itemmargin + _otherDotheight * (_dotCount - 1) + _currentDotheight;
            make.height.mas_equalTo(height);
        }];
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(2 * _leftMargin + _currentDotwidth);
        }];
    }
    else
    {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(2 * _topMargin + _currentDotheight);
        }];
        
        [_dotBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            CGFloat width = _leftMargin * 2 + (_dotCount - 1) * _itemmargin + _otherDotwidth * (_dotCount - 1) + _currentDotwidth;
            make.width.mas_equalTo(width);
        }];
    }
}

-(void)setDotCount:(NSInteger)dotCount
{
    _dotCount = dotCount;
    UIView *lastDotView = [[UIView alloc] initWithFrame:CGRectMake(_leftMargin, 0, 0, 0)];
    [_dotBgView addSubview:lastDotView];
    for (NSInteger i = 0; i < dotCount; ++i) {
        UIImageView *dotView = [[UIImageView alloc] init];
        dotView.tag = i;
        [_dotBgView addSubview:dotView];
        if (i == 0) {
            dotView.backgroundColor = _currentDotColor ? _currentDotColor : [UIColor whiteColor];
            
            dotView.image = [UIImage imageNamed:_currentDotImageName];
            if (_shapeType == CircleType) {
                dotView.layer.cornerRadius = _currentDotheight / 2;
            }
            if (_locationType == RightCenterType) {
                [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(lastDotView.mas_bottom).mas_offset(_itemmargin);
                    make.centerX.mas_equalTo(_dotBgView.mas_centerX).mas_offset(0);
                    make.width.mas_equalTo(_currentDotwidth);
                    make.height.mas_equalTo(_currentDotheight);
                }];
            }
            else
            {
                [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(lastDotView.mas_right).mas_offset(_itemmargin);
                    make.centerY.mas_equalTo(_dotBgView.mas_centerY).mas_offset(0);
                    make.width.mas_equalTo(_currentDotwidth);
                    make.height.mas_equalTo(_currentDotheight);
                }];
            }
            
            _currentDotView = dotView;
        }
        else
        {
            dotView.backgroundColor = _normalDotColor ? _normalDotColor : [UIColor lightGrayColor];
            if (_shapeType == CircleType) {
                dotView.layer.cornerRadius = _otherDotheight / 2;
            }
            
            dotView.image = [UIImage imageNamed:_otherDotImageName];
            if (_locationType == RightCenterType) {
                [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(lastDotView.mas_bottom).mas_offset(_itemmargin);
                    make.centerX.mas_equalTo(_dotBgView.mas_centerX).mas_offset(0);
                    make.width.mas_equalTo(_otherDotwidth);
                    make.height.mas_equalTo(_otherDotheight);
                }];
            }
            else
            {
                [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(lastDotView.mas_right).mas_offset(_itemmargin);
                    make.centerY.mas_equalTo(_dotBgView.mas_centerY).mas_offset(0);
                    make.width.mas_equalTo(_otherDotwidth);
                    make.height.mas_equalTo(_otherDotheight);
                }];
            }
        }
        lastDotView = dotView;
        [self.dotViewArr addObject:dotView];
        dotView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        [dotView addGestureRecognizer:tapGesture];
    }
    [self updateDotBgViewWidth];
}

-(void)tapGestureAction:(UITapGestureRecognizer *)gesture
{
    UIView *tapView = gesture.view;
    if (self.CurrentDotIndexChangedBlock) {
        self.CurrentDotIndexChangedBlock(tapView.tag);
    }
}

-(void)setCurrentDotIndex:(NSInteger)currentDotIndex
{
    _currentDotIndex = currentDotIndex;
    if (currentDotIndex < _dotViewArr.count) {
        UIImageView *imageView = _dotViewArr[currentDotIndex];
        for (UIImageView *object in _dotViewArr) {
            if (object == imageView) {
                _currentDotView = object;
                if (_shapeType == CircleType) {
                    _currentDotView.layer.cornerRadius = _currentDotwidth / 2;
                }
                
                if (!_currentDotImageName && [_currentDotImageName isKindOfClass:[NSString class]] && _currentDotImageName.length > 0) {
                    object.image = IMAGE(_currentDotImageName);
                }
                if (_currentDotColor) {
                    object.backgroundColor = _currentDotColor;
                }
                else
                {
                    object.backgroundColor = [UIColor whiteColor];
                }
                [object mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(_currentDotwidth);
                    make.height.mas_equalTo(_currentDotheight);
                }];
            }
            else
            {
                if (_shapeType == CircleType) {
                    _currentDotView.layer.cornerRadius = _otherDotwidth / 2;
                }
                
                if (!_otherDotImageName && [_otherDotImageName isKindOfClass:[NSString class]] && _otherDotImageName.length > 0) {
                    object.image = IMAGE(_otherDotImageName);
                }
                if (_normalDotColor) {
                    object.backgroundColor = _normalDotColor;
                }
                else
                {
                    object.backgroundColor = [UIColor lightGrayColor];
                }
                [object mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(_otherDotwidth);
                    make.height.mas_equalTo(_otherDotheight);
                }];
            }
        }
    }


//    if (autoCenterFrame) {
//        CGFloat selfWidth = 2 * _leftMargin + ((_dotCount - 1) * (_width + _itemmargin)) + _width;
//        CGFloat selfHeight = _height + 2 * _topMargin;
//        self.frame = CGRectMake(S_WIDTH / 2 - selfWidth / 2, S_HEIGHT - _selfBottomMargin - selfHeight, selfWidth, selfHeight);
//    }
}

-(NSMutableArray *)dotViewArr
{
    if (!_dotViewArr) {
        _dotViewArr = [NSMutableArray new];
    }
    return _dotViewArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
